/**
 * 本 AiLiServlet 为商品的主 AiLiServlet ，包含搜索商品，查看商品详情的功能。
 *
 *
 */

package com.webshop.DataBase;

import com.webshop.JavaBean.Category;
import com.webshop.JavaBean.Goods;
import com.webshop.JavaBean.Goods_attr;
import com.webshop.JavaBean.Picture;
import com.webshop.Utils.ServletOut;
import com.webshop.service.UltimateServie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/GoodsServlet")
public class GoodsServlet extends HttpServlet {
    UltimateServie us = new UltimateServie();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String method = request.getParameter("method");
        switch (method) {
            case "search":
                //最常用的方法，搜索包含关键字的商品。
                search(request, response);
                break;
            case "subclass"://查询所有子类商品
                subclass(request, response);
                break;
            case "fathclass"://返回所有父类目录
                fathclass(request, response);
                break;
            case "ishot"://返回热销商品
                backHot(request, response);
                break;
            case "DetailProduct":
                detailPro(request, response);
                break;

            default:
                break;
        }
    }

    /**
     * 返回指定商品的所有属性信息，例如
     * 返回的都是商品的子类
     * 手机的4G,8G等的不同套餐。
     * @param request
     * @param response
     */
    private void detailPro(HttpServletRequest request, HttpServletResponse response) {
        String productID = request.getParameter("ProductID");
        String CategoryPID = request.getParameter("PID");//这里可以优化，减少前端工作量
        int PID = Integer.parseInt(CategoryPID);
        int pro = Integer.parseInt(productID);

        Goods good = us.backGood(pro);

        List<Category> categories = us.categoryInfo(good.getCategory());
        List<Goods_attr> goods_attrs = us.ProductDetails(productID);

        request.setAttribute("product", good);//加入单个商品的总的信息
        request.setAttribute("goods_attr", goods_attrs);//加入商品的详细属性
        request.setAttribute("father", categories);//加入商品类别

        System.out.println(good.toString());
        System.out.println("商品属性"+goods_attrs.toString());
        System.out.println("类别信息： "+ categories.toString());
        ServletOut.request(request,response,"/html/product.jsp");
    }

    /**
     *   case "ishot":
     * 首页返回6个热销商品
     * @param request
     * @param response
     */
    private void backHot(HttpServletRequest request, HttpServletResponse response) {
        List<Goods> goods = us.isHot();
        //获取了所有热销商品。返回给前端。
        request.setAttribute("hotgoods", goods);
        System.out.println(goods);
        ServletOut.request(request,response,"html/index.jsp");
    }

    /**
     * 返回所有的父类。
     * @param request
     * @param response
     */
    private void fathclass(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("进入fathclass");
        String fathclass = request.getParameter("fatherclass");
        System.out.println(fathclass);
        List<Category> categories = us.categoryInfo(fathclass);
        request.setAttribute("parent",categories);
        ServletOut.request(request, response, "/html/products-grid.jsp");
        System.out.println(categories);
    }


    /**
     *  case "subclass":
     * 按照类别查询，查询出所有子类的商品。
     * @param request
     * @param response
     */
    private void subclass(HttpServletRequest request, HttpServletResponse response) {
        String a = request.getParameter("categoryname");
        System.out.println(a);
        List<Goods> categories = us.categoryProduct(a);
        System.out.println(categories);
        request.setAttribute("SubProduct", categories);
        //request 返回。
        ServletOut.request(request, response, "/html/products-grid.jsp");
    }


    /**
     * case "serach":
     * 不论登录不登录，都可以进行搜索
     * 可能还需要分页显式。
     * 已经实现了分页，需要前台传数据
     * @param request
     * @param response
     */
    private void search(HttpServletRequest request, HttpServletResponse response) {
        String productName = request.getParameter("ProductName").trim();
        String PageOfProduct = request.getParameter("perpage").trim();
        String page = request.getParameter("page").trim();
        System.out.println("进入search");
        System.out.println(productName);
        System.out.println(PageOfProduct);
        System.out.println(page);
        List<Goods> goods = us.searchProducts(productName, PageOfProduct, page);
        //在页面内获得了所以的搜索结果，都是大商品。
        System.out.println(goods);

        //返回给调用页面
        request.setAttribute("SubProduct", goods);
        ServletOut.request(request, response, "/html/products-grid.jsp");
    }



}
